/****************************************************************************
 * 2025.8 小庆永不失眠
 ****************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Linq;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
    public partial class TreasuerChestPanel : UIElement, IController
	{
		ResLoader mResLoader = ResLoader.Allocate();
		private void Awake()
		{
			BtnSure.onClick.AddListener(()=>
			{
				Time.timeScale = 1.0f;
				this.Hide();
			});
			
		}

        private void OnEnable()
        {
			var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();


			var matchedPairedItems = expUpgradeSystem.Items.Where(item=>
			{
				if(item.CurrentLevel.Value>7)
				{
					var ContainsInPair = expUpgradeSystem.Pairs.ContainsKey(item.Key);
					var pairedItemKey = expUpgradeSystem.Pairs[item.Key];
					var pairedItemStartUpgrade = expUpgradeSystem.dic[pairedItemKey].CurrentLevel.Value > 0;
					var pairunlocked = expUpgradeSystem.PairedProperties[item.Key].Value;

					return ContainsInPair && pairedItemStartUpgrade && !pairunlocked;
				}
				return false;
			});
			if(matchedPairedItems.Any())
			{
				var item =  matchedPairedItems.ToList().GetRandomItem();
				Content.text = "<b>" + item.PairedName + "</b>\n" + item.PairedDescription;

				while(!item.UpgradeFinish)
				{
					item.UpGrade();
				}

				icon.sprite = mResLoader.LoadSync<SpriteAtlas>("icon").GetSprite(item.PairedIconName);
				icon.SetNativeSize();
				icon.Show();


				expUpgradeSystem.PairedProperties[item.Key].Value = true;
            }
			else
			{
                var upgradeItems = expUpgradeSystem.Items.Where(item => item.CurrentLevel.Value > 0 && !item.UpgradeFinish).ToList();
                if (upgradeItems.Any())
                {
                    var item = upgradeItems.GetRandomItem();
                    Content.text = item.Description;
                    icon.sprite = mResLoader.LoadSync<SpriteAtlas>("icon").GetSprite(item.IconName);
					icon.Show();
                    item.UpGrade();
                }
                else
                {
                    if (Global.Hp.Value < Global.MaxHp.Value)
                    {
                        if (Random.Range(0, 1.0f) < 0.2f)
                        {
                            Content.text = "恢复 1 血量";
                            AudioKit.PlaySound("Hp");
                            Global.Hp.Value++;
                            icon.Hide();
                            return;
                        }
                    }
                    Content.text = "增加 50 金币";
                    Global.Coin.Value += 50;
					icon.Hide();
                }
            }

            
        }

        protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }
    }
}